home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 11.0 KB | 459 lines | [TEXT/MPS ] |
- /*
- File: Buffer.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __BUFFER__
- #include "Buffer.h"
- #endif
-
- #ifndef __DEBUGASSERT__
- #include "DebugAssert.h"
- #endif
-
- #ifndef __DEBUGCONSTANTS__
- #include "DebugConstants.h"
- #endif
-
- #pragma segment Buffer
-
- class TDebugFlag;
- extern TDebugFlag chrisFlag;
-
- /***********************************|****************************************/
-
- ABuffer::ABuffer ()
- {
- }
-
- /***********************************|****************************************/
-
- ABuffer::~ABuffer ()
- {
- }
-
- /***********************************|****************************************/
-
- short
- CompareBuffers ( const void* aa, unsigned long alen, const void* bb, unsigned long blen )
- {
- register short diff = 0;
- unsigned long c = alen < blen ? alen : blen;
- const char* a = (char*) aa;
- const char* b = (char*) bb;
-
- while ( c-- > 0 )
- {
- diff = *a++ - *b++;
-
- if ( diff != 0 )
- break;
- }
-
- if ( diff == 0 )
- diff = (short) ( (long) alen - (long) blen );
-
- return diff;
- }
-
- /***********************************|****************************************/
-
- ABuffer&
- ABuffer::operator = ( const ABuffer& that )
- {
- if ( this != &that )
- {
- ReadFrom ( that.GetPhysicalStart (), that.GetPhysicalLength () );
- }
- else
- {
- ASSERT ( this != &that );
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- ABuffer::ReadFrom ( const void* source, unsigned long sourceLength )
- {
- unsigned long newLength = SetPhysicalLength ( sourceLength );
- ASSERT ( newLength >= sourceLength );
- unsigned long copiedLength = Minimum ( newLength, sourceLength );
- BlockMove ( source, (void*) GetPhysicalStart (), copiedLength );
- return copiedLength;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- ABuffer::WriteTo ( void* dest, unsigned long destLength ) const
- {
- unsigned long desiredLength = GetPhysicalLength ();
- ASSERT ( destLength >= desiredLength );
- unsigned long copiedLength = Minimum ( destLength, desiredLength );
- BlockMove ( GetPhysicalStart (), dest, copiedLength );
- return copiedLength;
- }
-
- /***********************************|****************************************/
-
- Boolean
- ABuffer::operator == ( const ABuffer& that ) const
- {
- return 0 == ::CompareBuffers ( GetPhysicalStart (), GetPhysicalLength (), that.GetPhysicalStart (), that.GetPhysicalLength () );
- }
-
- /***********************************|****************************************/
-
- void
- ABuffer::FillBuffer ( unsigned char c )
- {
- unsigned char* p = (unsigned char*) GetPhysicalStart ();
- unsigned long length = GetPhysicalLength ();
-
- while ( length-- > 0 )
- *p++ = c;
- }
-
- /***********************************|****************************************/
-
- #ifndef __FSTREAM__
- #include "FStream.h"
- #endif
-
- extern ostream& DumpHex (ostream& s, const void *p, unsigned long size);
-
- ostream&
- ABuffer::operator >> ( ostream& stream ) const
- {
- if ( chrisFlag.Flag ( kExtensiveBufferDescribe ) )
- {
- stream << "ABuffer @ " << (void*) this << "\n";
- stream << "\tGetPhysicalLength (): " << GetPhysicalLength () << "\n";
- stream << "\tGetPhysicalStart (): ";
- DumpHex ( stream, (char*) GetPhysicalStart (), GetPhysicalLength () );
- }
- else
- {
- stream << GetPhysicalLength () << " bytes @ " << (void*) GetPhysicalStart () << '\n';
- }
-
- return stream;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- CBuffer::CBuffer ():
- ABuffer (),
- fIsExternal ( false )
- {
- SetPhysicalLength ( sizeof ( unsigned long ) );
- }
-
- /***********************************|****************************************/
-
- CBuffer::CBuffer ( unsigned long length ):
- ABuffer (),
- fIsExternal ( false )
- {
- SetPhysicalLength ( length );
- }
-
- /***********************************|****************************************/
-
- CBuffer::CBuffer ( const void* source, unsigned long sourceLength ):
- ABuffer (),
- fIsExternal ( false )
- {
- SetPhysicalLength ( sourceLength );
- BlockMove ( (Ptr) source, (Ptr) GetPhysicalStart (), (Size) GetPhysicalLength () ); //removed const
- }
-
- /***********************************|****************************************/
-
- CBuffer::CBuffer ( const ABuffer& that ):
- ABuffer (),
- fIsExternal ( false )
- {
- SetPhysicalLength ( that.GetPhysicalLength () );
- BlockMove ( (Ptr) that.GetPhysicalStart (), (Ptr) GetPhysicalStart (), (Size) GetPhysicalLength () );
- }
-
- /***********************************|****************************************/
-
- CBuffer::CBuffer ( const CBuffer& that ):
- ABuffer (),
- fIsExternal ( false )
- {
- SetPhysicalLength ( that.GetPhysicalLength () );
- BlockMove ( (Ptr) that.GetPhysicalStart (), (Ptr) GetPhysicalStart (), (Size) GetPhysicalLength () );
- }
-
- /***********************************|****************************************/
-
- CBuffer&
- CBuffer::operator = ( const CBuffer& that )
- {
- if ( this != &that )
- {
- ReadFrom ( that.GetPhysicalStart (), that.GetPhysicalLength () );
- }
- else
- {
- ASSERT ( this != &that );
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- CBuffer::~CBuffer ()
- {
- if ( fIsExternal )
- DeallocatePtr( (Ptr) fType.fExternal.fBuffer );
- }
-
- /***********************************|****************************************/
-
- unsigned long
- CBuffer::SetPhysicalLength ( unsigned long length )
- {
- if ( fIsExternal )
- DeallocatePtr( (Ptr) fType.fExternal.fBuffer );
-
- if ( length > kBufferMaxInternalLength )
- {
- fType.fExternal.fBuffer = FAILNewPtr( length );
- if ( fType.fExternal.fBuffer )
- {
- #if debug
- memset ( fType.fExternal.fBuffer, '\0', (unsigned int) length );
- #endif
- fIsExternal = true;
- return fType.fExternal.fLength = length;
- }
- else
- {
- ASSERT(noErr == MemError ());
- fIsExternal = false;
- return fType.fInternal.fLength = kBufferMaxInternalLength;
- }
- }
- else
- {
- fIsExternal = false;
- return fType.fInternal.fLength = (unsigned char) length;
- }
- }
-
- /***********************************|****************************************/
-
- ostream&
- CBuffer::operator >> ( ostream& s ) const
- {
- ABuffer::operator >> ( s );
-
- #if debug
- if ( chrisFlag.Flag ( kExtensiveBufferDescribe ) )
- {
- s << "\tfIsExternal: " << (short) fIsExternal << '\n';
-
- if ( fIsExternal )
- {
- s << "\tfLength: " << fType.fExternal.fLength << '\n';
- s << "\tfBuffer: " << fType.fExternal.fBuffer;
- }
- else
- {
- s << "\tfLength: " << (short) fType.fInternal.fLength;
- // s << "\tfBuffer: " << fType.fInternal.fBuffer;
- }
- }
- #endif
-
- return s;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- CPrefixBuffer::CPrefixBuffer ( Prefix prefix, unsigned long logicalLength ):
- ABuffer (),
- fBuffer ( logicalLength + prefix ),
- fSource ( nil ),
- fPrefix ( prefix ),
- fUpdate ( false )
- {
- UpdatePrefixWithLength ( 0 );
- }
-
- /***********************************|****************************************/
-
- CPrefixBuffer::CPrefixBuffer ( const ABuffer& source, Prefix prefix ):
- ABuffer (),
- fBuffer ( (const char*) source.GetPhysicalStart () - prefix, source.GetPhysicalLength () + prefix ),
- fSource ( (ABuffer*) &source ),
- fPrefix ( prefix ),
- fUpdate ( false )
- {
- UpdatePrefixWithLength ( source.GetPhysicalLength () );
- }
-
- /***********************************|****************************************/
-
- CPrefixBuffer::CPrefixBuffer ( ABuffer& source, Boolean update, Prefix prefix ):
- ABuffer (),
- fBuffer ( (const char*) source.GetPhysicalStart () - prefix, source.GetPhysicalLength () + prefix ),
- fSource ( &source ),
- fPrefix ( prefix ),
- fUpdate ( update )
- {
- UpdatePrefixWithLength ( source.GetPhysicalLength () );
- }
-
- /***********************************|****************************************/
-
- CPrefixBuffer::CPrefixBuffer ( const void* source, unsigned long length, Prefix prefix ):
- ABuffer (),
- fBuffer ( (char*) source - prefix, length + prefix ),
- fSource ( nil ),
- fPrefix ( prefix ),
- fUpdate ( false )
- {
- UpdatePrefixWithLength ( length );
- }
-
- /***********************************|****************************************/
-
- void
- CPrefixBuffer::UpdatePrefixWithLength ( unsigned long length )
- {
- void* start = (void*) fBuffer.GetPhysicalStart ();
-
- switch ( fPrefix )
- {
- case kByte:
- *(unsigned char*) start = (unsigned char) length;
- break;
-
- case kWord:
- *(unsigned short*) start = (unsigned short) length;
- break;
-
- case kLong:
- *(unsigned long*) start = (unsigned long) length;
- break;
- }
- }
-
- /***********************************|****************************************/
-
- unsigned long
- CPrefixBuffer::SetLogicalLength ( unsigned long requestedLogicalLength )
- {
- unsigned long requestedPhysicalLength = requestedLogicalLength + fPrefix;
- unsigned long actualPhysicalLength = fBuffer.GetPhysicalLength ();
-
- if ( actualPhysicalLength < requestedPhysicalLength )
- {
- actualPhysicalLength = fBuffer.SetPhysicalLength ( requestedPhysicalLength );
- ASSERT_RETURN_ZERO ( actualPhysicalLength >= requestedPhysicalLength );
- }
-
- unsigned long actualLogicalLength = actualPhysicalLength - fPrefix;
- UpdatePrefixWithLength ( actualLogicalLength );
- return actualLogicalLength;
- }
-
- /***********************************|****************************************/
-
- unsigned long
- CPrefixBuffer::GetLogicalLength () const
- {
- switch ( fPrefix )
- {
- case kByte: return *(unsigned char*) fBuffer.GetPhysicalStart ();
- case kWord: return *(unsigned short*) fBuffer.GetPhysicalStart ();
- case kLong: return *(unsigned long*) fBuffer.GetPhysicalStart ();
- };
- return 0; //keep compiler happy
- }
-
- /***********************************|****************************************/
-
- CPrefixBuffer::~CPrefixBuffer ()
- {
- UpdateNow ();
- }
-
- /***********************************|****************************************/
-
- unsigned long
- CPrefixBuffer::UpdateNow ()
- {
- if ( fUpdate && fSource )
- return fSource->ReadFrom ( GetLogicalStart (), GetLogicalLength () );
- else
- return 0;
- }
-
- /***********************************|****************************************/
-
- ostream&
- CPrefixBuffer::operator >> ( ostream& s ) const
- {
- ABuffer::operator >> ( s );
-
- #if debug
- if ( chrisFlag.Flag ( kExtensiveBufferDescribe ) )
- {
- // s << "\tfBuffer: " << fBuffer << '\n';
- s << "\tfSource: " << fSource << '\n';
- s << "\tfPrefix: " << (short) fPrefix << '\n';
- s << "\tfUpdate: " << (short) fUpdate;
- }
- #endif
-
- return s;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- CWrapperBuffer::CWrapperBuffer ( void* source, unsigned long length ):
- ABuffer (),
- fSource ( source ),
- fLength ( length )
- {
- }
-
- /***********************************|****************************************/
-
- CWrapperBuffer::~CWrapperBuffer ()
- {
- }
-
- /***********************************|****************************************/
-
- unsigned long
- CWrapperBuffer::SetPhysicalLength ( unsigned long newLength )
- {
- ASSERT ( newLength == fLength);
-
- return fLength;
- }
-
- /***********************************|****************************************/
-